package com.cw.jeeyt.service.impl.system;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cw.jeeyt.service.bo.system.SysScaffoldConfigBo;
import com.cw.jeeyt.core.util.SessionUtil;
import com.cw.jeeyt.dao.intf.system.SysScaffoldConfigDao;
import com.cw.jeeyt.dao.model.system.SysScaffoldConfigDo;
import com.cw.jeeyt.dao.query.system.SysScaffoldConfigQuery;
import com.cw.jeeyt.service.converter.system.SysScaffoldConfigConverter;
import com.cw.jeeyt.service.intf.system.SysScaffoldConfigService;
import com.cw.lang.common.utils.SequenceTool;
import com.cw.lang.mybatis.dto.PageInfo;
import com.cw.lang.mybatis.dto.PageParam;

/**
 * 系统脚手架配置接口业务实现类
 *
 * @author admin
 * @date 2020-05-24 21:04:55
 */
@Service
public class SysScaffoldConfigServiceImpl implements SysScaffoldConfigService {
    @Autowired
    private SysScaffoldConfigDao sysScaffoldConfigDao;

    /**
     * 新增系统脚手架配置
     *
     * @param request 条件
     * @return Long
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long addSysScaffoldConfig(SysScaffoldConfigBo request) {
        if (request == null) {
            return null;
        }
        Long id = SequenceTool.nextLongId();
        request.setId(id);
        request.setCreateBy(SessionUtil.getSessionUserId());
        request.setUpdateBy(SessionUtil.getSessionUserId());
        request.setCreateTime(new Date());
        request.setUpdateTime(new Date());
        sysScaffoldConfigDao.insertBySelective(SysScaffoldConfigConverter.bo2Do(request));
        return id;
    }

    /**
     * 更新系统脚手架配置
     *
     * @param request 条件
     * @return boolean
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean modifySysScaffoldConfig(SysScaffoldConfigBo request) {
        if (request == null) {
            return false;
        }
        request.setUpdateBy(SessionUtil.getSessionUserId());
        request.setUpdateTime(new Date());
        sysScaffoldConfigDao.updateBySelective(SysScaffoldConfigConverter.bo2Do(request));
        return true;
    }

    /**
     * 删除系统脚手架配置
     *
     * @param request 条件
     * @return boolean
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteSysScaffoldConfig(SysScaffoldConfigBo request) {
        if (request == null) {
            return false;
        }
        return sysScaffoldConfigDao.deleteByKey(request.getId()) > 0;
    }

    /**
     * 分页查询系统脚手架配置
     *
     * @param request 条件
     * @return PageInfo<SysScaffoldConfigBo>
     */
    @Override
    public PageInfo<SysScaffoldConfigBo> selectPage(SysScaffoldConfigBo request, PageParam pageParam) {
        PageInfo<SysScaffoldConfigDo> pageInfo = sysScaffoldConfigDao.selectPage(SysScaffoldConfigQuery.builder()
            .id(request.getId())
            .scaffoldCode(request.getScaffoldCode())
            .scaffoldName(request.getScaffoldName())
            .pomGroupId(request.getPomGroupId())
            .pomArtifactId(request.getPomArtifactId())
            .pomName(request.getPomName())
            .pomVersion(request.getPomVersion())
            .basePackage(request.getBasePackage())
            .javaVersion(request.getJavaVersion())
            .springbootVersion(request.getSpringbootVersion())
            .implodedSpringcloud(request.getImplodedSpringcloud())
            .springcloudVersion(request.getSpringcloudVersion())
            .implodedAlibabaDubbo(request.getImplodedAlibabaDubbo())
            .alibabaDubboVersion(request.getAlibabaDubboVersion())
            .registryCenterType(request.getRegistryCenterType())
            .registryVersion(request.getRegistryVersion())
            .daoHandleType(request.getDaoHandleType())
            .daoHandleVersion(request.getDaoHandleVersion())
            .createBy(request.getCreateBy())
            .createTime(request.getCreateTime())
            .updateBy(request.getUpdateBy())
            .updateTime(request.getUpdateTime())
            .build(), pageParam);
        List<SysScaffoldConfigBo> sysScaffoldConfigs = SysScaffoldConfigConverter.doList2BoList(pageInfo.getList());
        return new PageInfo<>(pageInfo, sysScaffoldConfigs);
    }

    /**
     * 查询系统脚手架配置详情信息
     *
     * @param request 条件
     * @return SysScaffoldConfigBo
     */
    @Override
    public SysScaffoldConfigBo querySysScaffoldConfigDetail(SysScaffoldConfigBo request) {
        if (request == null) {
            return null;
        }
        return SysScaffoldConfigConverter.do2Bo(sysScaffoldConfigDao.selectByKey(request.getId()));
    }

    /**
     * 根据条件查询系统脚手架配置
     *
     * @param request 条件
     * @return List<SysScaffoldConfigBo>
     */
    @Override
    public List<SysScaffoldConfigBo> querySysScaffoldConfigList(SysScaffoldConfigBo request) {
        if (request == null) {
            return null;
        }
        return SysScaffoldConfigConverter.doList2BoList(sysScaffoldConfigDao.selectByExample(
            SysScaffoldConfigQuery.builder()
                .id(request.getId())
                .scaffoldCode(request.getScaffoldCode())
                .scaffoldName(request.getScaffoldName())
                .pomGroupId(request.getPomGroupId())
                .pomArtifactId(request.getPomArtifactId())
                .pomName(request.getPomName())
                .pomVersion(request.getPomVersion())
                .basePackage(request.getBasePackage())
                .javaVersion(request.getJavaVersion())
                .springbootVersion(request.getSpringbootVersion())
                .implodedSpringcloud(request.getImplodedSpringcloud())
                .springcloudVersion(request.getSpringcloudVersion())
                .implodedAlibabaDubbo(request.getImplodedAlibabaDubbo())
                .alibabaDubboVersion(request.getAlibabaDubboVersion())
                .registryCenterType(request.getRegistryCenterType())
                .registryVersion(request.getRegistryVersion())
                .daoHandleType(request.getDaoHandleType())
                .daoHandleVersion(request.getDaoHandleVersion())
                .createBy(request.getCreateBy())
                .createTime(request.getCreateTime())
                .updateBy(request.getUpdateBy())
                .updateTime(request.getUpdateTime())
                .build()));
    }

    /**
     * 根据key查询系统脚手架配置
     *
     * @param id
     * @return
     */
    @Override
    public SysScaffoldConfigBo selectByKey(Long id) {

        return SysScaffoldConfigConverter.do2Bo(sysScaffoldConfigDao.selectByKey(id));
    }

    /**
     * 生成脚手架
     *
     * @param sysScaffoldConfigBo
     * @return
     */
    @Override
    public Boolean generateScaffold(SysScaffoldConfigBo sysScaffoldConfigBo) {
        //步骤
        //1.根据artifactId，basePackage生成脚手架目录
        //2.替换脚手架文件freemarker占位符
        //3.目录打包下载
        return true;
    }
}
